package com.example.coffeeseller_2.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.View;

import com.example.coffeeseller_2.R;
import com.example.coffeeseller_2.utils.Waiter;

/**
 * Created by WH on 2018/8/13.
 */
public class MyWaveView extends View {
    private Context context;
    private Thread thread;
    private int paintColor;
    private int step = 10;
    private int startColor;
    private int centerColor;
    private int endColor;
    private boolean isStop = false;
    private OnInVilidateListenner onInVilidateListenner;
    private Path drawPath;
    private Paint paint;
    private int measuredWidth;
    private int measuredHeight;
    private int mode;

    public MyWaveView(Context context) {
        super(context);
    }

    public MyWaveView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        TypedArray attrsArray = context.obtainStyledAttributes(attrs, R.styleable.MyWaveView);

        paintColor = attrsArray.getColor(R.styleable.MyWaveView_waveViewColor,
                Color.WHITE);
        step = attrsArray.getInteger(R.styleable.MyWaveView_waveViewStep, 10);

        startColor = attrsArray.getColor(R.styleable.MyWaveView_waveViewStartColor,
                Color.WHITE);

        centerColor = attrsArray.getColor(R.styleable.MyWaveView_waveViewCenterColor,
                Color.WHITE);

        endColor = attrsArray.getColor(R.styleable.MyWaveView_waveViewEndColor,
                Color.WHITE);

        mode = attrsArray.getInteger(R.styleable.MyWaveView_waveViewMode, 0);

        onInVilidateListenner = new OnInVilidateListenner() {
            @Override
            public void invalidate(Path path) {
                drawPath = path;
                postInvalidate();
            }
        };

        paint = new Paint();
        paint.setColor(paintColor);
        paint.setAntiAlias(true);
        paint.setAlpha(150);
        paint.setStyle(Paint.Style.FILL);
    }

    private void startThread() {
        if (thread == null) {
            thread = new Thread(new MyThread());
            thread.start();
        }
    }

    private void stopThread() {
        if (thread != null) {
            thread = null;
        }
    }

    public MyWaveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setStop(boolean isStop) {
        this.isStop = isStop;
        if (isStop) {
            stopThread();
        } else {
            startThread();
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        /**
         * 修改波浪的宽度
         */
        measuredWidth = getMeasuredWidth();
        /**
         * 修改波浪的高度
         */
        measuredHeight = getMeasuredHeight();

        int[] colors = new int[]{
                startColor, centerColor, endColor
        };
        LinearGradient lg = new LinearGradient(0, 0, measuredWidth, measuredHeight
                , colors, null, Shader.TileMode.MIRROR);
        paint.setShader(lg);

        startThread();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (drawPath != null) {
            canvas.drawPath(drawPath, paint);
        }

    }

    public class MyThread extends Thread {
        @Override
        public void run() {
            super.run();

            /**
             * 总共平移的间隔
             */
            int totalWidth = 0;

            while (!isStop) {
                /*try {
                    sleep(step);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
                Waiter.doWait(step);
                try {

                    Path path = null;
                    if (mode == 0) {
                        path = getSinPath(totalWidth, 5, 1);
                    } else if (mode == 1) {
                        path = getSinPath(totalWidth, 1, 5);
                    }

                    if (onInVilidateListenner != null) {
                        onInVilidateListenner.invalidate(path);
                    }
                    totalWidth += step;

                    if (totalWidth > getMeasuredWidth()) {
                        totalWidth = 0;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                }
            }

        }

        @NonNull
        private Path getSinPath(int totalWidth, int start, int end) {
            Path path = new Path();

            path.moveTo(-measuredWidth + totalWidth, measuredHeight * 3 / 8);

            path.quadTo(-measuredWidth * 7 / 8 + totalWidth, measuredHeight * start / 8,
                    -measuredWidth * 6 / 8 + totalWidth, measuredHeight * 3 / 8);

            path.quadTo(-measuredWidth * 5 / 8 + totalWidth, measuredHeight * end / 8,
                    -measuredWidth * 4 / 8 + totalWidth, measuredHeight * 3 / 8);

            path.quadTo(-measuredWidth * 3 / 8 + totalWidth, measuredHeight * start / 8,
                    -measuredWidth * 2 / 8 + totalWidth, measuredHeight * 3 / 8);

            path.quadTo(-measuredWidth * 1 / 8 + totalWidth, measuredHeight * end / 8,
                    0 + totalWidth, measuredHeight * 3 / 8);


            path.quadTo(measuredWidth * 1 / 8 + totalWidth, measuredHeight * start / 8,
                    measuredWidth * 2 / 8 + totalWidth, measuredHeight * 3 / 8);

            path.quadTo(measuredWidth * 3 / 8 + totalWidth, measuredHeight * end / 8,
                    measuredWidth * 4 / 8 + totalWidth, measuredHeight * 3 / 8);

            path.quadTo(measuredWidth * 5 / 8 + totalWidth, measuredHeight * start / 8,
                    measuredWidth * 6 / 8 + totalWidth, measuredHeight * 3 / 8);

            path.quadTo(measuredWidth * 7 / 8 + totalWidth, measuredHeight * end / 8,
                    measuredWidth * 8 / 8 + totalWidth, measuredHeight * 3 / 8);

            path.lineTo(measuredWidth + totalWidth, measuredHeight);
            //  if ()
            path.lineTo(-measuredWidth + totalWidth, measuredHeight);
            path.close();
            return path;
        }

    }

    interface OnInVilidateListenner {
        void invalidate(Path path);
    }
}
